<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="运算符自增 只能后置，不允许前置 只能作为单独语句，不可以作为表达式  1234x :&#x3D; 1x++++x &#x2F;&#x2F; errory :&#x3D; x++ &#x2F;&#x2F; error    指针指针是一个专门用来保存地址的整形变量。 go语言的指针相比于c中的指针有很大的限制和改变。  支持相等运算符，但是不支持加减法和类型转换。 不使用-&gt;访问成员，而是使用. 不是所有对象都能使用&amp;获得其地址。  1234">
<meta property="og:type" content="article">
<meta property="og:title" content="GoLang-运算符、流程控制">
<meta property="og:url" content="http://example.com/2022/11/24/GoLang-%E8%A1%A8%E8%BE%BE%E5%BC%8F/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:description" content="运算符自增 只能后置，不允许前置 只能作为单独语句，不可以作为表达式  1234x :&#x3D; 1x++++x &#x2F;&#x2F; errory :&#x3D; x++ &#x2F;&#x2F; error    指针指针是一个专门用来保存地址的整形变量。 go语言的指针相比于c中的指针有很大的限制和改变。  支持相等运算符，但是不支持加减法和类型转换。 不使用-&gt;访问成员，而是使用. 不是所有对象都能使用&amp;获得其地址。  1234">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-11-24T07:00:17.000Z">
<meta property="article:modified_time" content="2022-11-29T18:04:51.398Z">
<meta property="article:author" content="SongyangJi">
<meta property="article:tag" content="Go">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2022/11/24/GoLang-%E8%A1%A8%E8%BE%BE%E5%BC%8F/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2022/11/24/GoLang-%E8%A1%A8%E8%BE%BE%E5%BC%8F/","path":"2022/11/24/GoLang-表达式/","title":"GoLang-运算符、流程控制"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>GoLang-运算符、流程控制 | JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">JsyBlog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">1.</span> <span class="nav-text">运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E5%A2%9E"><span class="nav-number">1.1.</span> <span class="nav-text">自增</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8C%87%E9%92%88"><span class="nav-number">1.2.</span> <span class="nav-text">指针</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#unsafe-Pointer%E5%92%8Cuintptr"><span class="nav-number">1.2.1.</span> <span class="nav-text">unsafe.Pointer和uintptr</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9B%B6%E9%95%BF%E5%BA%A6%E5%AF%B9%E8%B1%A1"><span class="nav-number">1.2.2.</span> <span class="nav-text">零长度对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E7%BA%A7%E6%8C%87%E9%92%88"><span class="nav-number">1.2.3.</span> <span class="nav-text">二级指针</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-number">2.</span> <span class="nav-text">初始化</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6"><span class="nav-number">3.</span> <span class="nav-text">流程控制</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#if-else"><span class="nav-number">3.1.</span> <span class="nav-text">if-else</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#switch"><span class="nav-number">3.2.</span> <span class="nav-text">switch</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#for"><span class="nav-number">3.3.</span> <span class="nav-text">for</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#for-range"><span class="nav-number">3.3.1.</span> <span class="nav-text">for-range</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#continue%E3%80%81break"><span class="nav-number">3.4.</span> <span class="nav-text">continue、break</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/11/24/GoLang-%E8%A1%A8%E8%BE%BE%E5%BC%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="GoLang-运算符、流程控制 | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          GoLang-运算符、流程控制
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-11-24 15:00:17" itemprop="dateCreated datePublished" datetime="2022-11-24T15:00:17+08:00">2022-11-24</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-11-30 02:04:51" itemprop="dateModified" datetime="2022-11-30T02:04:51+08:00">2022-11-30</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Go/" itemprop="url" rel="index"><span itemprop="name">Go</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h1><h2 id="自增"><a href="#自增" class="headerlink" title="自增"></a>自增</h2><ol>
<li>只能后置，不允许前置</li>
<li>只能作为单独语句，不可以作为表达式</li>
</ol>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">x := <span class="number">1</span></span><br><span class="line">x++</span><br><span class="line">++x <span class="comment">// error</span></span><br><span class="line">y := x++ <span class="comment">// error</span></span><br></pre></td></tr></table></figure>



<h2 id="指针"><a href="#指针" class="headerlink" title="指针"></a>指针</h2><p>指针是一个专门用来保存地址的整形变量。</p>
<p>go语言的指针相比于c中的指针有很大的限制和改变。</p>
<ol>
<li><strong>支持相等</strong>运算符，但是<strong>不支持加减法和类型转换</strong>。</li>
<li>不使用<code>-&gt;</code>访问成员，而是使用<code>.</code></li>
<li>不是所有对象都能使用<code>&amp;</code>获得其地址。</li>
</ol>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">x := <span class="number">1</span></span><br><span class="line"><span class="keyword">var</span> p *<span class="type">int</span> = &amp;x</span><br><span class="line">fmt.Println(x, p)</span><br><span class="line"><span class="comment">//p++ // Invalid operation: p++ (non-numeric type *int)</span></span><br><span class="line"><span class="comment">//p += 2 // Invalid operation: p += 2 (mismatched types *int and untyped int)</span></span><br></pre></td></tr></table></figure>



<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">m := <span class="built_in">make</span>(<span class="keyword">map</span>[<span class="type">string</span>]<span class="type">int</span>)</span><br><span class="line">m[<span class="string">&quot;1&quot;</span>] = <span class="number">1</span></span><br><span class="line">m[<span class="string">&quot;2&quot;</span>] = <span class="number">2</span></span><br><span class="line"><span class="built_in">println</span>(m, &amp;m) <span class="comment">// 0x1400005e180 0x1400000e028 为什么不一样？0x1400005e180引用着map</span></span><br><span class="line">fmt.Println(m, &amp;m) <span class="comment">// map[1:1 2:2] &amp;map[1:1 2:2]</span></span><br></pre></td></tr></table></figure>




<h3 id="unsafe-Pointer和uintptr"><a href="#unsafe-Pointer和uintptr" class="headerlink" title="unsafe.Pointer和uintptr"></a>unsafe.Pointer和uintptr</h3><h3 id="零长度对象"><a href="#零长度对象" class="headerlink" title="零长度对象"></a>零长度对象</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a, b <span class="keyword">struct</span>&#123;&#125;</span><br><span class="line">pa := &amp;a</span><br><span class="line">pb := &amp;b</span><br><span class="line"><span class="built_in">println</span>(pa)</span><br><span class="line"><span class="built_in">println</span>(pb)</span><br><span class="line"><span class="built_in">println</span>(pa == pb) <span class="comment">// 结果和具体实现有关</span></span><br><span class="line"></span><br><span class="line">p1 := unsafe.Pointer(pa)</span><br><span class="line">p2 := unsafe.Pointer(pb)</span><br><span class="line"><span class="keyword">var</span> p1ptr <span class="type">uintptr</span> = <span class="type">uintptr</span>(p1)</span><br><span class="line"><span class="keyword">var</span> p2ptr <span class="type">uintptr</span> = <span class="type">uintptr</span>(p2)</span><br><span class="line"><span class="built_in">println</span>(p1ptr)</span><br><span class="line"><span class="built_in">println</span>(p2ptr)</span><br><span class="line"><span class="built_in">println</span>(p1ptr == p2ptr) <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<p>输出：</p>
<p>0x14000096f58<br>0x14000096f58<br>false<br>1374390153048<br>1374390153048<br>true</p>
<p><strong>零长度对象</strong>的地址如何分配看具体实现，总之<strong>肯定不为nil</strong>，换言之它拥有合法的内存地址。</p>
<h3 id="二级指针"><a href="#二级指针" class="headerlink" title="二级指针"></a>二级指针</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">x := <span class="number">1</span></span><br><span class="line">px := &amp;x</span><br><span class="line">ppx := &amp;px <span class="comment">// var pxx **int = &amp;px</span></span><br><span class="line">fmt.Println(x, &amp;x)</span><br><span class="line">fmt.Println(px, &amp;px, *px)</span><br><span class="line">fmt.Println(ppx, &amp;ppx, *ppx)</span><br></pre></td></tr></table></figure>



<h1 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h1><p>对于复合类型（数组、切片、字典、结构体）的初始化而言，有一些语法限制。</p>
<ol>
<li>初始化表达式必须有类型标签；</li>
<li>左花括号必须在类型尾部，不能另起一行；</li>
<li>多个成员以逗号分隔；</li>
</ol>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> data <span class="keyword">struct</span> &#123;</span><br><span class="line">	x <span class="type">int</span></span><br><span class="line">	s <span class="type">string</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">d := data&#123; <span class="comment">// 初始化表达式必须有类型标签</span></span><br><span class="line">	<span class="number">1</span>,</span><br><span class="line">	<span class="string">&quot;s&quot;</span>, <span class="comment">// 逗号不能省略</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h1 id="流程控制"><a href="#流程控制" class="headerlink" title="流程控制"></a>流程控制</h1><h2 id="if-else"><a href="#if-else" class="headerlink" title="if-else"></a>if-else</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">t</span><span class="params">()</span></span> <span class="type">int</span> &#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="number">0</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">   x := <span class="number">1</span></span><br><span class="line">   <span class="keyword">if</span> x &gt; <span class="number">1</span> &#123; <span class="comment">// </span></span><br><span class="line">      </span><br><span class="line">   &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">   </span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="keyword">if</span> a, b := t(), t(); a &lt; b &#123;</span><br><span class="line">      </span><br><span class="line">   &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      </span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h2 id="switch"><a href="#switch" class="headerlink" title="switch"></a>switch</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">a, b, c := <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span></span><br><span class="line"><span class="keyword">switch</span> x := foo(); x &#123; <span class="comment">// 支持局部变量初始化</span></span><br><span class="line"><span class="keyword">case</span> a: <span class="comment">// 支持非常量</span></span><br><span class="line">    <span class="built_in">println</span>(a)</span><br><span class="line"><span class="keyword">case</span> b:</span><br><span class="line">    <span class="built_in">println</span>(b)</span><br><span class="line"><span class="keyword">case</span> c:</span><br><span class="line">    <span class="built_in">println</span>(c)</span><br><span class="line"><span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">    <span class="built_in">println</span>(<span class="number">4</span>)</span><br><span class="line"><span class="keyword">case</span> <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>: <span class="comment">// 支持多case合并</span></span><br><span class="line">    <span class="built_in">println</span>(<span class="string">&quot;5 &lt;= x &lt;= 7&quot;</span>)</span><br><span class="line"><span class="keyword">default</span>:</span><br><span class="line">    <span class="built_in">println</span>(<span class="string">&quot;8 &lt;= x&quot;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><strong>无需显式使用break</strong>，这是和Java中的switch最大的区别。</p>
<p>如果需要Java中的switch中<strong>贯通后续</strong>的效果，可以使用<code>fallthrough</code>关键字（后续就不再匹配条件表达式，注意执行下一个case），同时使用<code>break</code>拦截fallthrough。 </p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">a, b, c := <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span></span><br><span class="line"><span class="keyword">switch</span> x := foo(); x &#123; <span class="comment">// 支持局部变量初始化</span></span><br><span class="line"><span class="keyword">case</span> a: <span class="comment">// 支持非常量</span></span><br><span class="line">    <span class="built_in">println</span>(a)</span><br><span class="line"><span class="keyword">case</span> b:</span><br><span class="line">    <span class="built_in">println</span>(b)</span><br><span class="line">    <span class="keyword">if</span> b &gt; <span class="number">1</span> &#123;</span><br><span class="line">       <span class="keyword">break</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">fallthrough</span></span><br><span class="line"><span class="keyword">case</span> c:</span><br><span class="line">    <span class="built_in">println</span>(c)</span><br><span class="line"><span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">    <span class="built_in">println</span>(<span class="number">4</span>)</span><br><span class="line"><span class="keyword">case</span> <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>: <span class="comment">// 支持多case合并</span></span><br><span class="line">    <span class="built_in">println</span>(<span class="string">&quot;5 &lt;= x &lt;= 7&quot;</span>)</span><br><span class="line"><span class="keyword">default</span>:</span><br><span class="line">    <span class="built_in">println</span>(<span class="string">&quot;8 &lt;= x&quot;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="for"><a href="#for" class="headerlink" title="for"></a>for</h2><p>for循环的几种形式：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"> x := <span class="number">10</span></span><br><span class="line"> <span class="keyword">for</span> x &gt; <span class="number">0</span> &#123;</span><br><span class="line">   x--</span><br><span class="line"> &#125;</span><br><span class="line">   </span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="number">10</span>; i++ &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> &#123;</span><br><span class="line">	<span class="comment">// endless loop</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> <span class="literal">true</span> &#123;</span><br><span class="line">	<span class="comment">// endless loop</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="for-range"><a href="#for-range" class="headerlink" title="for-range"></a>for-range</h3><p>可以使用<code>for-range</code>完成可迭代对象的数据遍历，支持<strong>字符串、数组、数组指针、切片、字典、通道</strong>，返回索引，键值数据。</p>
<table>
<thead>
<tr>
<th>Data-type</th>
<th>1st value</th>
<th>2nd value</th>
</tr>
</thead>
<tbody><tr>
<td>string</td>
<td>index</td>
<td>s[index]</td>
</tr>
<tr>
<td>array</td>
<td>index</td>
<td>v[index]</td>
</tr>
<tr>
<td>map</td>
<td>key</td>
<td>value</td>
</tr>
<tr>
<td>channel</td>
<td>element</td>
<td></td>
</tr>
</tbody></table>
<p>这里只演示string和array 的遍历，后面的具体再介绍。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">&quot;abcdef&quot;</span></span><br><span class="line"><span class="keyword">for</span> i, c := <span class="keyword">range</span> str &#123;</span><br><span class="line">	fmt.Printf(<span class="string">&quot;s[%d] = %c i.e. %c\n&quot;</span>, i, str[i], c)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>for-range类似于java中的for-each语句，但是由于语言底层本身的区别，二者还是有不同的。</p>
<p>比如，range数组和数组指针是有差别的。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">f1</span><span class="params">()</span></span> &#123;</span><br><span class="line">	data := [<span class="number">3</span>]<span class="type">string</span>&#123;<span class="string">&quot;1&quot;</span>, <span class="string">&quot;2&quot;</span>, <span class="string">&quot;3&quot;</span>&#125;</span><br><span class="line">	<span class="keyword">for</span> i, s := <span class="keyword">range</span> data &#123;</span><br><span class="line">		<span class="keyword">if</span> i == <span class="number">0</span> &#123;</span><br><span class="line">			data[<span class="number">0</span>] += <span class="string">&quot;a&quot;</span></span><br><span class="line">			data[<span class="number">1</span>] += <span class="string">&quot;b&quot;</span></span><br><span class="line">			data[<span class="number">2</span>] += <span class="string">&quot;c&quot;</span></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">println</span>(i, s, data[i]) <span class="comment">// 这里输出的s都是改变之前的data的复制品中的元素</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">f2</span><span class="params">()</span></span> &#123;</span><br><span class="line">	data := [<span class="number">3</span>]<span class="type">string</span>&#123;<span class="string">&quot;1&quot;</span>, <span class="string">&quot;2&quot;</span>, <span class="string">&quot;3&quot;</span>&#125;</span><br><span class="line">	<span class="keyword">for</span> i, s := <span class="keyword">range</span> &amp;data &#123;</span><br><span class="line">		<span class="keyword">if</span> i == <span class="number">0</span> &#123;</span><br><span class="line">			data[<span class="number">0</span>] += <span class="string">&quot;a&quot;</span></span><br><span class="line">			data[<span class="number">1</span>] += <span class="string">&quot;b&quot;</span></span><br><span class="line">			data[<span class="number">2</span>] += <span class="string">&quot;c&quot;</span></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">println</span>(i, s, data[i])</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	f1()</span><br><span class="line">	<span class="built_in">println</span>()</span><br><span class="line">	f2()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">0 1 1a</span></span><br><span class="line"><span class="comment">1 2 2b</span></span><br><span class="line"><span class="comment">2 3 3c</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">0 1 1a</span></span><br><span class="line"><span class="comment">1 2b 2b</span></span><br><span class="line"><span class="comment">2 3c 3c</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>



<p><strong>range会复制目标数据，比如range一个数组时其实遍历的是复制的那个数组。不过，字符串、切片是个很小的结构体，字典和通道本身是指针封装，它们的复制成本都很小，无需担心。</strong></p>
<p>返回部分元素。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">data := [<span class="number">3</span>]<span class="type">string</span>&#123;<span class="string">&quot;1&quot;</span>, <span class="string">&quot;2&quot;</span>, <span class="string">&quot;3&quot;</span>&#125;</span><br><span class="line"><span class="keyword">for</span> i := <span class="keyword">range</span> data &#123;</span><br><span class="line">    <span class="keyword">if</span> i == <span class="number">0</span> &#123;</span><br><span class="line">        data[<span class="number">0</span>] += <span class="string">&quot;a&quot;</span></span><br><span class="line">        data[<span class="number">1</span>] += <span class="string">&quot;b&quot;</span></span><br><span class="line">        data[<span class="number">2</span>] += <span class="string">&quot;c&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> _, s := <span class="keyword">range</span> data &#123;</span><br><span class="line">    <span class="built_in">println</span>(s)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> <span class="keyword">range</span> data &#123; <span class="comment">// 仅迭代，不返回。可以用来执行清空channel</span></span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h2 id="continue、break"><a href="#continue、break" class="headerlink" title="continue、break"></a>continue、break</h2><p>continue 仅用于 for 循环。</p>
<p>break可以用于for、switch，还可以用于<code>select</code>语句（后面详细介绍）</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Go/" rel="tag"># Go</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/11/21/GoLang-%E7%B1%BB%E5%9E%8B/" rel="prev" title="GoLang-类型">
                  <i class="fa fa-chevron-left"></i> GoLang-类型
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/11/25/GoLang-%E5%87%BD%E6%95%B0/" rel="next" title="GoLang-函数">
                  GoLang-函数 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
